package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.cldr.nmg.LocaleNames_nmg;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        //将网页传输过来的密码加密 因为数据库里面的密码就是加密状态
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        //对比查询是否成功
        if (one == null){
            //查询失败
            log.info("用户名或密码错误");
            return R.error("用户名或密码错误");
        }

        //对比密码是否正确
        if (!one.getPassword().equals(password)){
            //表示查询不对
            log.info("用户名或密码错误");
            return R.error("用户名或密码错误");
        }
        //查看员工的状态是否被禁用
        if (one.getStatus() == 0){
            //员工状态被禁用
            log.info("账号被禁用");
            return R.error("账号被禁用");
        }
        //程序能进行至此 表示全部成功
        //将员工id放入Session 返回
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R logout(HttpServletRequest request){
        log.info("用户退出");
        //退出 并且删除Session
        request.getSession().invalidate();
        return R.success();
    }

    //新增员工信息
/*    @PostMapping
    public R save(HttpServletRequest request , @RequestBody Employee employee){
        log.info("新增员工");
        //设置默认密码 123456  并且加密
        employee.setPassword(DigestUtils.md5DigestAsHex("1234556".getBytes()));

        //给当前 赋值
         //获得sesson
//        Long emplId = (Long) request.getSession().getAttribute("employee");
//
//        employee.setStatus(1);
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateUser(emplId);
//        employee.setUpdateUser(emplId);

        employeeService.save(employee);
        return R.success("新增员工成功");
    }*/


    @PostMapping
    public R save(@RequestBody Employee employee){
        //网页只给了四条信息
        //除了自动填充的信息之外还有状态  密码

        employee.setPassword(DigestUtils.md5DigestAsHex("12345".getBytes()));
        employee.setStatus(1);

        employeeService.save(employee);
        return R.success("新增员工成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page, Integer pageSize, String name){
        log.info("分页查询");
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        //构造查询条件
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断名字是否为空 然后查询名字
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //排序 按照时间排序
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }

    //禁用
    @PutMapping
    //获取Session
    public R update(HttpServletRequest request ,@RequestBody Employee employee){
        log.info("更新员工");

        //获取当前登录用户信息 获取的是ID
//        Long employeeId = (Long) request.getSession().getAttribute("employee");
//
//        //根据ID进行跟新操作
//        //不用再改static了 因为前段已经改过了
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(employeeId);

        employeeService.updateById(employee);
        return R.success();
    }

    //编辑信息
    //因为路径是一个变量 所以要用PathVariable
    @GetMapping("/{id}")
    public R<Employee> getId(@PathVariable  Long id){
        log.info("根据id查询信息 并显示");

        //根据service里面的getById方法查找剩下的信息
        Employee emp = employeeService.getById(id);

        if (emp != null){
            //证明查到了
            return R.success(emp);
        }
        //没有查到
        return R.error("该用户不存在");
    }

}
